<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *空调管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'edit','title'=>'新建小区户型'),	
);
$onepluginname='jy_kongtiaoshop';
//空调分类
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['shequid']=$_RQ['id'];
		$return['params']['id']=$_RQ['id'];
		$_SESSION['nowid']=$_RQ['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$tip='[温馨提示]：不同的门店附近有不同的小区户型！';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kongtiaoshop_home',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['pcd']=json_decode($item['pcd'],true);
			$item['shequtitle']=pdo_getcolumn('jy_kongtiaoshop_shequ',array('id'=>$item['shequid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'小区户型列表'),
			array('op'=>'edit','title'=>'添加小区户型'),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'户型名称','type'=>'string'),
			array('name'=>'image','title'=>'户型图片','type'=>'image'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'hometypeid','title'=>'建筑类型','type'=>'select','options'=>'hometypes'),
			array('name'=>'shequtitle','title'=>'所属小区','type'=>'string'),
			array('name'=>'mianji','title'=>'房间总面积','type'=>'unitstring','unit'=>'m²'),
			array('name'=>'peibi','title'=>'配比','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'onewfee','title'=>'单位面积负荷','type'=>'unitstring','unit'=>'W/m²'),
			array('name'=>'wfee','title'=>'总冷负荷','type'=>'unitstring','unit'=>'W'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'/peilist/','title'=>'配置方案'),
		)),
		'search_one'=>array('name'=>'title','title'=>'户型名称','add'=>false),
	);
	$return['status']=array('未启用','已启用');
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['hometypes']=pdo_getall('jy_kongtiaoshop_home_type',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kongtiaoshop_home',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$shequ=pdo_get('jy_kongtiaoshop_shequ',array('id'=>$_SESSION['nowid'],'uniacid'=>$_SESSION['uniacid']));
		$item=array(
			'mianji'=>intval($shequ['mianji']),
			'peibi'=>trim($shequ['peibi']),
			'onewfee'=>intval($shequ['onewfee']),
			'wfee'=>intval($shequ['wfee']),
		);
	}
	$item['houseinfo']=json_decode($item['houseinfo'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'户型名称','type'=>'string'),
			array('name'=>'image','title'=>'户型图片','type'=>'image'),
			array('name'=>'housenum','title'=>'N房','type'=>'unitstring','unit'=>'房'),
			array('name'=>'tingnum','title'=>'N厅','type'=>'unitstring','unit'=>'房'),
			array(
				'type'=>'array',
				'name'=>'houseinfo',
				'title'=>'房间信息',
				'add_tip'=>'添加一个房间',
				'list'=>array(
					array('type'=>'string','name'=>'title','title'=>'房间名称','help'=>'比如：客厅，主卧，次卧，书房'),
					array('name'=>'mianji','title'=>'房间面积','type'=>'unitstring','unit'=>'m²'),			
				),
				'details'=>array(
					array('type'=>'string','name'=>'title','title'=>'房间名称','help'=>'比如：客厅，主卧，次卧，书房'),
					array('name'=>'mianji','title'=>'房间面积','type'=>'unitstring','unit'=>'m²'),	
				)
			),
			array('name'=>'peibi','title'=>'配比','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'onewfee','title'=>'单位面积负荷','type'=>'unitstring','unit'=>'W/m²'),
			//array('name'=>'wfee','title'=>'总冷负荷','type'=>'unitstring','unit'=>'W'),
			array('name'=>'open','title'=>'公共？','type'=>'switch','help'=>'开启后，其他门店有可以借用本户型！'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['status']=array('未启用','已启用');
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['hometypes']=pdo_getall('jy_kongtiaoshop_home_type',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('标题不能为空','error');
	}
	$shequ=pdo_get('jy_kongtiaoshop_shequ',array('id'=>$_SESSION['nowid'],'uniacid'=>$_SESSION['uniacid']));
	$mianji=0;
	$wfee=0;
	foreach($postdata['houseinfo'] as $mv){
		$mianji +=$mv['mianji'];
		$wfee +=$mv['mianji']*$postdata['onewfee']*$postdata['peibi'];
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'peibi'=>trim($postdata['peibi']),
		'onewfee'=>intval($postdata['onewfee']),
		'wfee'=>$wfee,
		'open'=>intval($postdata['open']),
		'status'=>intval($postdata['status']),
		'image'=>trim($postdata['image']),
		'housenum'=>intval($postdata['housenum']),
		'tingnum'=>intval($postdata['tingnum']),
		'houseinfo'=>ijson_encode($postdata['houseinfo']),
		'mianji'=>$mianji,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_kongtiaoshop_home',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		$data['shequid']=$_SESSION['nowid'];
		$data['storeid']=$shequ['storeid'];
		$data['hometypeid']=$shequ['hometypeid'];
		$data['createtime']=time();
		pdo_insert('jy_kongtiaoshop_home',$data);
		$insertid=pdo_insertid();
	}
	exi('操作成功！','','goback');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kongtiaoshop_home',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}